package com.syh.jn.scm.service.impl.downstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.base.logistics.DownLogistics;
import com.syh.jn.scm.domain.downstream.entity.base.logistics.DownLogisticsAddress;
import com.syh.jn.scm.domain.downstream.entity.base.logistics.DownLogisticsTruck;
import com.syh.jn.scm.domain.downstream.repository.base.DownLogisticsRepository;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsMto;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsSto;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsVo;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressMto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressSto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressVo;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckMto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckSto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckVo;
import com.syh.jn.scm.service.api.downstream.base.DownLogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 物流接口实现
 */
@Service
@Transactional
public class DownLogisticsServiceImpl extends AbstractServiceImpl<DownLogisticsSto, DownLogisticsMto, DownLogisticsVo, DownLogistics, Long> implements DownLogisticsService {

  @Autowired
  private DownLogisticsRepository downLogisticsRepositoryImpl;

  @Override
  protected AggregateRepository<DownLogistics, Long> getAggregateRepository() {
    return downLogisticsRepositoryImpl;
  }

  /**
   * 增加物流信息
   * @param sto
   * @return
   */
  @Override
  public Long save(DownLogisticsSto sto) {
    DownLogistics logistics = new DownLogistics(sto);
    Long id = downLogisticsRepositoryImpl.add(logistics);
//
//    DownLogistics logisticsMq = downLogisticsRepositoryImpl.getById(id);
//    Object[] obj = new Object[]{logisticsMq};
//    //发送事件
//    this.publicEvent(DownLogisticsEvent.buildDownLogisticsEventAddDestory(obj));

    return id;
  }

  /**
   * 修改物流信息
   * @param mto
   */
  @Override
  public void update(DownLogisticsMto mto) {
    DownLogistics logistics = downLogisticsRepositoryImpl.getById(mto.getId());
    logistics.modify(mto);
    downLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 删除物流信息
   * @param id
   */
  @Override
  public void delete(Long id) {
    DownLogistics logistics = downLogisticsRepositoryImpl.getById(id);
    logistics.destroy();
    downLogisticsRepositoryImpl.remove(logistics);
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    DownLogistics logistics = downLogisticsRepositoryImpl.getById(id);
    logistics.enabled();
    downLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    DownLogistics logistics = downLogisticsRepositoryImpl.getById(id);
    logistics.disabled();
    downLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 批量增加地址
   * @param stoList
   */
  @Override
  public void batchSaveAddress(List<DownLogisticsAddressSto> stoList) {
    List<DownLogisticsAddress> addressesList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      DownLogisticsAddress address = new DownLogisticsAddress(sto);
      addressesList.add(address);
    });
    downLogisticsRepositoryImpl.batchSaveAddress(addressesList);
  }

  /**
   * 批量修改地址
   * @param mtoList
   */
  @Override
  public void batchModifyAddress(List<DownLogisticsAddressMto> mtoList) {
    List<DownLogisticsAddress> addressesList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      DownLogisticsAddress address = downLogisticsRepositoryImpl.getByAddressId(mto.getId());
      address.modify(mto);
      addressesList.add(address);
    });
    downLogisticsRepositoryImpl.batchModifyAddress(addressesList);
  }

  /**
   * 根据地址id删除
   * @param addressId
   */
  @Override
  public void deleteAddress(Long addressId) {
    DownLogisticsAddress address = downLogisticsRepositoryImpl.getByAddressId(addressId);
    address.destroy();
    downLogisticsRepositoryImpl.deleteAddress(address);
  }

  /**
   * 批量添加服务车型
   * @param stoList
   */
  @Override
  public void batchSaveTruck(List<DownLogisticsTruckSto> stoList) {
    List<DownLogisticsTruck> truckList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      DownLogisticsTruck truck = new DownLogisticsTruck(sto);
      truckList.add(truck);
    });
    downLogisticsRepositoryImpl.batchSaveTruck(truckList);
  }

  /**
   * 批量修改服务车型
   * @param mtoList
   */
  @Override
  public void batchModifyTruck(List<DownLogisticsTruckMto> mtoList) {
    List<DownLogisticsTruck> truckList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      DownLogisticsTruck truck = downLogisticsRepositoryImpl.getByTruckId(mto.getId());
      truck.modify(mto);
      truckList.add(truck);
    });
    downLogisticsRepositoryImpl.batchModifyTruck(truckList);
  }

  /**
   * 根据车型id删除
   * @param truckId
   */
  @Override
  public void deleteTruck(Long truckId) {
    DownLogisticsTruck truck = downLogisticsRepositoryImpl.getByTruckId(truckId);
    truck.destroy();
    downLogisticsRepositoryImpl.deleteTruck(truck);
  }

  /**
   * 根据物流id查地址列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<DownLogisticsAddressVo> getAddressListById(Long logisticsId) {
    return downLogisticsRepositoryImpl.getAddressListById(logisticsId);
  }

  /**
   * 根据物流id查车型列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<DownLogisticsTruckVo> getTruckListById(Long logisticsId) {
    return downLogisticsRepositoryImpl.getTruckListById(logisticsId);
  }

  /**
   * 根据车型id获取车型
   * @param truckId
   * @return
   */
  @Override
  public DownLogisticsTruckVo getByTruckId(Long truckId) {
    DownLogisticsTruck entity = downLogisticsRepositoryImpl.getByTruckId(truckId);
    DownLogisticsTruckVo vo = new DownLogisticsTruckVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }

  /**
   * 根据地址id获取地址
   * @param addressId
   * @return
   */
  @Override
  public DownLogisticsAddressVo getByAddressId(Long addressId) {
    DownLogisticsAddress entity = downLogisticsRepositoryImpl.getByAddressId(addressId);
    DownLogisticsAddressVo vo = new DownLogisticsAddressVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }
}
